package com.geek2.supermarket.controller;


import com.geek2.supermarket.pojo.SmbmsBill;
import com.geek2.supermarket.pojo.SmbmsProvider;
import com.geek2.supermarket.service.BillService;
import com.geek2.supermarket.service.BillServiceImpl;
import com.geek2.supermarket.service.ProviderService;
import com.geek2.supermarket.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/bill/")
public class BillController {

    @Autowired
    private ProviderService providerService;
    @Autowired
    private BillServiceImpl billService;

    @RequestMapping("queryAll")
    public String queryAll(String queryProductName, String queryProviderId, String queryIsPayment, String pageIndex, Model model) {
        System.out.println(queryProductName + "|id|" + queryProviderId + "||" + queryIsPayment + "||" + pageIndex);
        String queryName = null;
        int currentPageNo = 1;
        int providerId = 0;
        int isPayment=0;
        if (queryProductName != null && !queryProductName.isEmpty()) {
            queryName = queryProductName;
        }
        if (queryProviderId!=null) {
            providerId=Integer.parseInt(queryProviderId);
        }
        if (queryIsPayment!=null) {
            isPayment=Integer.parseInt(queryIsPayment);
        }
       
        if (pageIndex != null) {
            currentPageNo = Integer.parseInt(pageIndex);
        }

        //总数量（表）
        int totalCount =billService.getBillCount(queryName, providerId, isPayment);
        
        //总页数支持
        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(4);
        pageSupport.setTotalCount(totalCount);
        //总页数（表）
        int totalPageCount = pageSupport.getTotalPageCount();
        List<SmbmsBill> smbmsBills = billService.queryAll(queryName, providerId, isPayment, currentPageNo,pageSupport.getPageSize());
        List<SmbmsProvider> smbmsProviders = providerService.queryAll();
        model.addAttribute("providerList", smbmsProviders);
        model.addAttribute("billList", smbmsBills);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", pageSupport.getCurrentPageNo());
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("queryProductName", queryName);
        model.addAttribute("queryProviderId", queryProviderId);
        model.addAttribute("queryIsPayment", queryIsPayment);
        return "/WEB-INF/jsp/bill/billlist";
    }

}